package com.ruoyi.web.controller.admin.home;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.dto.HomeBannerDTO;
import com.ruoyi.system.dto.HomeBannerQueryDTO;
import com.ruoyi.system.entity.HomeBannerDO;
import com.ruoyi.system.service.HomeBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 首页Banner轮播图配置控制器
 */
@RestController
@RequestMapping("/admin/home/banner")
public class HomeBannerController {

    @Resource
    private HomeBannerService homeBannerService;

    @PostMapping("/add")
    public AjaxResult addBanner(@Valid @RequestBody HomeBannerDTO bannerDTO) {
        Long bannerId = homeBannerService.addBanner(bannerDTO);
        return AjaxResult.success(bannerId);
    }

    @PutMapping("/update")
    public AjaxResult updateBanner(@Valid @RequestBody HomeBannerDTO bannerDTO) {
        homeBannerService.updateBanner(bannerDTO);
        return AjaxResult.success();
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult deleteBanner(@PathVariable Long id) {
        homeBannerService.deleteBanner(id);
        return AjaxResult.success();
    }

    @GetMapping("/{id}")
    public AjaxResult getBannerById(@PathVariable Long id) {
        HomeBannerDTO bannerDTO = homeBannerService.getBannerById(id);
        return AjaxResult.success(bannerDTO);
    }

    @ApiOperation("分页查询Banner列表")
    @GetMapping("/page")
    public AjaxResult queryBannerPage(HomeBannerQueryDTO queryDTO) {
        IPage<HomeBannerDO> pageResult = homeBannerService.queryBannerPage(queryDTO);
        return AjaxResult.success(pageResult);
    }

    @ApiOperation("更新Banner排序")
    @PutMapping("/{id}/sort")
    public AjaxResult updateBannerSort(@PathVariable Long id, @RequestParam Integer newSort) {
        homeBannerService.updateBannerSort(id, newSort);
        return AjaxResult.success();
    }

    @ApiOperation("更新Banner状态")
    @PutMapping("/{id}/status")
    public AjaxResult updateBannerStatus(@PathVariable Long id, @RequestParam Integer status) {
        homeBannerService.updateBannerStatus(id, status);
        return AjaxResult.success();
    }
}
